package faktorI;

import glavni.Menadzer;
import glavni.RadnaPovrs;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.*;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import faktorI.FaktorIToolBar;

public class FaktorIRadnaPovrs extends RadnaPovrs {
	
	private static FaktorIRadnaPovrs instanca;
	private  JTextArea[] prikazi = new JTAWithBackgroundImage[4];
	private JButton[] tasteri = new JButton[7];
	private JDialog dijalog;
	private JTextArea prikaz;
	FaktorIToolBar toolBar;
	JScrollPane[] klizaci;
	
	public static FaktorIRadnaPovrs instanca(){return instanca;}
	
	public FaktorIRadnaPovrs(Menadzer m){
		super(m);
		File file = new File("files/icons/background.JPG");
		File file1 = new File("files/icons/background1.JPG");
		toolBar = new FaktorIToolBar(this);
		instanca = this;
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension dimension = toolkit.getScreenSize();
		klizaci = new JScrollPane[4];
		try{
			prikazi[0] = new JTAWithBackgroundImage(file);
		}catch(Exception e){prikazi[0] = new JTextArea();}
		prikazi[0].setEditable(false);
		prikazi[0].addFocusListener(new FocusListener(){
			public void focusGained(FocusEvent arg0) {
				klizaci[0].setBorder(BorderFactory.createTitledBorder(BorderFactory
						.createLineBorder(Color.black, 6), "Pravila",
						TitledBorder.CENTER, TitledBorder.TOP, Font
								.decode("Serif-ITALIC-28"), Color.black));
				prikazi[0].setFont(Font.decode("Dialog-13"));
				instanca.iscrtaj();
				
			}
			public void focusLost(FocusEvent arg0) {
				klizaci[0].setBorder(BorderFactory.createTitledBorder(BorderFactory
						.createLineBorder(Color.black, 3), "Pravila",
						TitledBorder.CENTER, TitledBorder.TOP, Font
								.decode("Serif-ITALIC-20"), Color.black));
				prikazi[0].setFont(Font.decode("Dialog-12"));
				instanca.iscrtaj();
			}
			
		});
		prikazi[0].addMouseListener(new MouseAdapter(){
			 public void mouseClicked(MouseEvent e) {
			     if(e.getClickCount() == 2){
			    	 tasteri[0].doClick();
			    }
			 }
		});
		klizaci[0] = new JScrollPane(prikazi[0]);
		klizaci[0].setPreferredSize(new Dimension(
				(int) ((dimension.width - 150) / 3),
				(int) ((dimension.height -200))));
		add(klizaci[0],BorderLayout.WEST);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2, 1));
		try{
			prikazi[1] = new JTAWithBackgroundImage(file1);
		}catch(Exception e){prikazi[1] = new JTextArea();}
		prikazi[1].setEditable(false);
		prikazi[1].addFocusListener(new FocusListener(){
			public void focusGained(FocusEvent arg0) {
				klizaci[1].setBorder(BorderFactory.createTitledBorder(BorderFactory
						.createLineBorder(Color.black, 6), "Opažanja",
						TitledBorder.CENTER, TitledBorder.TOP, Font
								.decode("Serif-ITALIC-28"), Color.black));
				prikazi[1].setFont(Font.decode("Dialog-13"));
				instanca.iscrtaj();
				
			}
			public void focusLost(FocusEvent arg0) {
				klizaci[1].setBorder(BorderFactory.createTitledBorder(BorderFactory
						.createLineBorder(Color.black, 3), "Opažanja",
						TitledBorder.CENTER, TitledBorder.TOP, Font
								.decode("Serif-ITALIC-20"), Color.black));
				prikazi[1].setFont(Font.decode("Dialog-12"));
				instanca.iscrtaj();
			
			}
			
		});

		
		prikazi[1].addMouseListener(new MouseAdapter(){
			 public void mouseClicked(MouseEvent e) {
			     if(e.getClickCount() == 2){
			    	 tasteri[1].doClick();
			    }
			 }
		});
		
		klizaci[1] = new JScrollPane(prikazi[1]);
		klizaci[1].setPreferredSize(new Dimension(
				(int) ((dimension.width - 150) / 3),
				(int) ((dimension.height - 200)/4)));
		panel.add(klizaci[1]);
		
		try{
			prikazi[2] = new JTAWithBackgroundImage(file1);
		}catch(Exception e){prikazi[2] = new JTextArea();}
		prikazi[2].setEditable(false);
		prikazi[2].addFocusListener(new FocusListener(){
			public void focusGained(FocusEvent arg0) {
				klizaci[2].setBorder(BorderFactory.createTitledBorder(BorderFactory
						.createLineBorder(Color.black, 6), "Zaključak",
						TitledBorder.CENTER, TitledBorder.TOP, Font
								.decode("Serif-ITALIC-28"), Color.black));
				prikazi[2].setFont(Font.decode("Dialog-13"));
				instanca.iscrtaj();
				
			}
			public void focusLost(FocusEvent arg0) {
				klizaci[2].setBorder(BorderFactory.createTitledBorder(BorderFactory
						.createLineBorder(Color.black, 3), "Zaključak",
						TitledBorder.CENTER, TitledBorder.TOP, Font
								.decode("Serif-ITALIC-20"), Color.black));
				prikazi[2].setFont(Font.decode("Dialog-12"));
				instanca.iscrtaj();
			
			}
			
		});	
		
		prikazi[2].addMouseListener(new MouseAdapter(){
			 public void mouseClicked(MouseEvent e) {
			     if(e.getClickCount() == 2){
			    	 tasteri[2].doClick();
			    }
			 }
		});
		klizaci[2] = new JScrollPane(prikazi[2]);
		klizaci[2].setPreferredSize(new Dimension(
				(int) ((dimension.width - 150) / 3),
				(int) ((dimension.height - 200)/ 2)));
		panel.add(klizaci[2]);
		add(panel, BorderLayout.CENTER);
		
		panel = new JPanel();
		panel.setLayout(new BorderLayout());

		try{
			prikazi[3] = new JTAWithBackgroundImage(file1);
		}catch(Exception e){prikazi[3] = new JTextArea();}
		prikazi[3].setEditable(false);
		prikazi[3].addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ke) {
				if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
					tasteri[4].doClick();
				}
				else if(ke.getKeyCode() == KeyEvent.VK_RIGHT){
					tasteri[5].doClick();
				}
				else if(ke.getKeyCode() == KeyEvent.VK_UP){
					tasteri[6].doClick();
				}
				else if(ke.getKeyCode() == KeyEvent.VK_DOWN){
					tasteri[3].doClick();
				}
			}
		});
		prikazi[3].addFocusListener(new FocusListener(){
			public void focusGained(FocusEvent arg0) {
				klizaci[3].setBorder(BorderFactory.createTitledBorder(BorderFactory
						.createLineBorder(Color.black, 6), "Koraci",
						TitledBorder.CENTER, TitledBorder.TOP, Font
								.decode("Serif-ITALIC-28"), Color.black));
				prikazi[3].setFont(Font.decode("Dialog-13"));
				instanca.iscrtaj();
				
			}
			public void focusLost(FocusEvent arg0) {
				klizaci[3].setBorder(BorderFactory.createTitledBorder(BorderFactory
						.createLineBorder(Color.black, 3), "Koraci",
						TitledBorder.CENTER, TitledBorder.TOP, Font
								.decode("Serif-ITALIC-20"), Color.black));
				prikazi[3].setFont(Font.decode("Dialog-12"));
				instanca.iscrtaj();
			
			}
			
		});		
		prikazi[3].addMouseListener(new MouseAdapter(){
			 public void mouseClicked(MouseEvent e) {
			     if(e.getClickCount() == 2){
			    	 tasteri[3].doClick();
			     }
			     else if(e.getClickCount() == 1){
			    	 tasteri[5].doClick();
			     }
			 }
		});
		
		klizaci[3] = new JScrollPane(prikazi[3]);
		klizaci[3].setPreferredSize(new Dimension(
				(int) ((dimension.width - 150) / 3),
				(int) ((dimension.height - 256))));
		panel.add(klizaci[3], BorderLayout.NORTH);

		
			JPanel panel1 = new JPanel();		
			panel1.setLayout(new GridLayout(1, 4));
			tasteri[3] = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				"files/icons/backward.JPG")));
			tasteri[3].setEnabled(false);
			tasteri[4] = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				"files/icons/rplay.JPG")));
			tasteri[4].setEnabled(false);
			tasteri[5] = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				"files/icons/play.JPG")));
			tasteri[5].setEnabled(false);
			tasteri[6] = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				"files/icons/forward.JPG")));
			tasteri[6].setEnabled(false);
			panel1.add(tasteri[3]);
			panel1.add(tasteri[4]);
			panel1.add(tasteri[5]);
			panel1.add(tasteri[6]);
			
		panel.add(panel1, BorderLayout.SOUTH);
		add(panel,BorderLayout.EAST);
		
		klizaci[0].setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(Color.black, 3), "Pravila",
				TitledBorder.CENTER, TitledBorder.TOP, Font
						.decode("Serif-ITALIC-20"), Color.black));
		klizaci[1].setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(Color.black, 3), "Opažanja",
				TitledBorder.CENTER, TitledBorder.TOP, Font
						.decode("Serif-ITALIC-20"), Color.black));
		klizaci[2].setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(Color.black, 3), "Zaključak",
				TitledBorder.CENTER, TitledBorder.TOP, Font
						.decode("Serif-ITALIC-20"), Color.black));
		klizaci[3].setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(Color.black, 3), "Koraci",
				TitledBorder.CENTER, TitledBorder.TOP, Font
						.decode("Serif-ITALIC-20"), Color.black));

		panel = new JPanel();
		tasteri[0] = new JButton("Definiši pravila",new ImageIcon(Toolkit.getDefaultToolkit().getImage(
			"files/icons/def.JPG")));
		tasteri[1] = new JButton("Definiši opažanja", new ImageIcon(Toolkit.getDefaultToolkit().getImage(
			"files/icons/opaz.JPG")));
		tasteri[2] = new JButton("Definiši zaključak",new ImageIcon(Toolkit.getDefaultToolkit().getImage(
			"files/icons/zaklj.JPG")));
		
		tasteri[0].setFont(Font.decode("Serif-ITALIC-20"));
		tasteri[0].setPreferredSize(new Dimension(
				(int) ((dimension.width - 150) / 3), 50));
		panel.add(tasteri[0]);
		
		tasteri[1].setFont(Font.decode("Serif-ITALIC-20"));
		tasteri[1].setPreferredSize(new Dimension(
				(int) ((dimension.width - 150) / 3), 50));
		tasteri[1].setEnabled(false);
		panel.add(tasteri[1]);

		tasteri[2].setFont(Font.decode("Serif-ITALIC-20"));
		tasteri[2].setPreferredSize(new Dimension(
				(int) ((dimension.width - 150) / 3), 50));
		tasteri[2].setEnabled(false);
		
		panel.add(tasteri[2]);
		
		add("South", panel);
		prikaz = new JTextArea();
		prikaz.setBackground(Color.getColor(null));
		prikaz.setEditable(false);
		menadzer.setTitle("Ekspertski sistemi");
		menadzer.setSize(dimension);
		menadzer.setLocation(0, 0);
		menadzer.addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseMoved(MouseEvent e) {
				menadzer.setLocation(0, 0);
			}
		});

		menadzer.setResizable(false);

		simulacija = new FaktorISimulacija();
		
		tasteri[0].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				menadzer.zabraniNaPocetak();
				menadzer.zabraniKorakNapred();
				menadzer.zabraniKorakNazad();
				menadzer.zabraniNaKraj();
				LstOpaz.instanca().isprazni();
				LstZaklj.instanca().isprazni();
				prikazi[1].setText("");
				prikazi[2].setText("");
				dijalog = new DijalogPravila();
				dijalog.setVisible(true);
			}
		});
		tasteri[1].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				menadzer.zabraniNaPocetak();
				menadzer.zabraniKorakNapred();
				menadzer.zabraniKorakNazad();
				menadzer.zabraniNaKraj();
				if(!LstPrav.instanca().prazna()){
					try{
					dijalog = new DijalogOpazanje();
					dijalog.setVisible(true);
					}catch(Greska g){new GreskaD(instanca,""+g);}
				}
				else {
					JOptionPane.showMessageDialog(menadzer,
							"Morate prvo korektno definisati pravila\n"
								+ " da bi ste mogli definisati opažanja!",
								"Obavestenje", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		tasteri[2].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				if(!LstPrav.instanca().prazna()){
					dijalog = new DijalogZakljucak();
					dijalog.setVisible(true);
				}
				else {
					JOptionPane.showMessageDialog(menadzer,
							"Morate prvo korektno definisati pravila\n"
									+ " da bi ste mogli definisati zaključak!",
							"Obaveštenje", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		tasteri[3].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				simulacija.naPocetak();
				prikazi[3].requestFocus();
				iscrtaj();
			}
		});
		
		tasteri[4].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				simulacija.korakNazad();
				prikazi[3].requestFocus();
				iscrtaj();
			}
		});
		tasteri[5].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				simulacija.korakNapred();
				prikazi[3].requestFocus();
				iscrtaj();
			}
		});
		tasteri[6].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				simulacija.naKraj();
				prikazi[3].requestFocus();
				iscrtaj();
			}
		});
				
		menadzer.dozvoliSnimi();
		menadzer.zabraniNaPocetak();
		menadzer.zabraniKorakNapred();
		menadzer.zabraniKorakNazad();
		menadzer.zabraniNaKraj();
	} 
	
	public boolean startnoStanjeKompletno() {
		if (!LstPrav.instanca().prazna()&& !LstOpaz.instanca().prazna()&& LstZaklj.instanca().postojiSelektovan())
			return true;
		else
			return false;
	}
	
	public void getObjasnjenja() {
		Objasnjenja.instanca().setVisible(true);
	}
	public void napraviIzvestaj(){	      
		Izvestaj.napraviIzvestaj();
	} 
	
	public void novaSimulacija() {
		((FaktorIToolBar)FaktorIRadnaPovrs.instanca().dohvatiToolBar()).zabraniIzvestaj();
		LstPrav.instanca().refresh();
		LstZaklj.instanca().isprazni();
		LstOpaz.instanca().isprazni();
		KontrolerPetlji.isprazni();
		simulacija = new FaktorISimulacija();
		menadzer.zabraniNaPocetak();
		menadzer.zabraniKorakNapred();
		menadzer.zabraniKorakNazad();
		menadzer.zabraniNaKraj();
		tasteri[3].setEnabled(false);
		tasteri[4].setEnabled(false);
		tasteri[5].setEnabled(false);
		tasteri[6].setEnabled(false);
		prikaz.setText(null);
		prikazi[1].setText(null);
		prikazi[2].setText(null);
		prikazi[3].setText(null);
		tasteri[0].setEnabled(true);
		if(!LstPrav.instanca().prazna()){
			tasteri[1].setEnabled(true);
			tasteri[2].setEnabled(true);
		}
		else{
			tasteri[1].setEnabled(false);
			tasteri[2].setEnabled(false);
		}
	}
	
	public JDialog getDijalog(){return dijalog;}

	public JTextArea dohvatiPrikaz(int i) {
		return prikazi[i];
	}

	public JToolBar dohvatiToolBar() {
		return toolBar;
	}
	
	public JButton dohvatiTaster(int i) {
		return tasteri[i];
	}
	
	public Menadzer dohvatiMenadzera() {
		return menadzer;
	}
	
	public PomocOSimulatoru dohvatiPomoc() {
		return new PomocOSimulatoru();
	}
	
	public String dohvatiEkstenziju() {
		return "faktorI";
	}

	public FileFilter dohvatiFilter() {
		return new FileNameExtensionFilter(
				"Simulacija faktora izvesnosnti (*.faktorI)", "faktorI");
	}

	public void iscrtaj() {
		((FaktorISimulacija) simulacija).refresh();
	}
}
